#!/usr/bin/python
# -*- coding:utf-8 -*-
# edit 2014/11/03
'''
'''
import os
import time
from CreateCovXml import CreateCovXml
import xlrd

'''
' 获取联系方式值
'''

def getAddrValue(row_value):
    addr_value = []
    # 0 id
    addr_value.append(str(row_value[0]))
    # 1 consignee
    addr_value.append(row_value[2])
    # 2 address
    addr_value.append(row_value[3])
    # 3 district 区
    addr_value.append(row_value[5])
    # 4 city
    addr_value.append("")
    # 5 province
    addr_value.append("")
    # 6 coutry
    addr_value.append("CN")
    # 7 zipcode
    addr_value.append(str(int(row_value[4])))
    # 8 tel
    addr_value.append(str(int(row_value[6])))
    # 9 mobile
    addr_value.append(str(int(row_value[6])))
    # 10 email
    addr_value.append("")
    # 11 shipping_name
    addr_value.append("")
    # 12 postscript
    addr_value.append("")
    # 13 order_sn
    addr_value.append(str(int(row_value[0])))
    # 14 reference
    addr_value.append(row_value[7])
    # 15 weight
    addr_value.append(int(row_value[1]))

    return addr_value

'''
'获取物品信息
'
'''
def getGoodsValue(row_value, id):
    good = []
    # 'id'
    good.append(id)
    # 'name'
    good.append(row_value[12])
    # 'weight'
    good.append(row_value[10])
    # 'number'
    good.append(int(row_value[9]))
    # 'price'
    good.append(row_value[11])
    # 15 statno
    good.append(str(int(row_value[8])))
    return good

def getValues(excel):
    data = xlrd.open_workbook(excel)
    table = data.sheets()[0]
    nrows = table.nrows
    addr_values = []
    goods = []
    id =""
    for i in range(1, nrows):
        if table.row_values(i)[0] != "":
            id = str(table.row_values(i)[0])
            addr_values.append(getAddrValue(table.row_values(i)))
            goods.append(getGoodsValue(table.row_values(i), id))
        else:
            print 'add more',table.row_values(i)
            goods.append(getGoodsValue(table.row_values(i), id))
            print goods
        #print table.row_values(i)
    return addr_values, goods
            
if __name__ == "__main__":
    excel = 'shipment.xlsx'
    addr_values, goods_values = getValues(excel)
    for value in addr_values:
        goods = []
        weight = value[15]
        reference = value[14]
        for goods_value in goods_values:
            if value[0] == goods_value[0]:
                goods.append(goods_value)
                print goods_value
            if value[0] < goods_value[0]:
                break
        print goods
            
        if(len(goods) > 0):  # 有货则生成保单
            path = str(time.strftime("%Y%m%d")) + "/"
            xmlfile = CreateCovXml(path + str(value[0]) + ".xml")  # 创建xml
            if not os.path.exists(path):  # 若没有路径，则新建路径
                try:
                    os.makedirs(path)
                except Exception, s:
                    print 'new dir wrong', s, '\n', path
            xmlfile.begin_cov(str(value[13]))
            xmlfile.creat_shipment(reference)
            xmlfile.add_weight(str(weight))
            for good in goods:
                xmlfile.add_goods(good)
            xmlfile.create_receiver(value)
            xmlfile.end_cov()
    print 'endl'

